package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.bean.Log;
import cn.bean.User;
import cn.dao.LogDao;
import cn.dao.PermissionDao;
import cn.dao.UserDao;
import cn.dbc.BaseDao;

public class UserDaoImpl implements UserDao
{

	@Override
	public User findUserById(int id)
	{
		User u=new User();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		BaseDao bd=new BaseDao();
		conn=bd.getConnection();
		if(conn!=null)
		{
			String sql="select * from user where id=?";		
			try
			{
				pstmt=conn.prepareStatement(sql);
				pstmt.setInt(1, id);
				rs=pstmt.executeQuery();
				
				PermissionDao pd=new PermissionDaoImpl();
                LogDao ld=new LogDaoImpl();
				while(rs.next())
				{
					u.setId(rs.getInt(1));
					u.setName(rs.getString(2));
					u.setPhone(rs.getString(3));
					u.setMoney(rs.getDouble(4));
					u.setPermission(pd.findPermissionById(rs.getInt(5)));
					u.setLog(ld.findLogById(rs.getInt(6)));			
				}
				
			} catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				bd.closeConnection(conn, pstmt, rs);
			}

		}
		return u;
	}

	@Override
	public boolean doUser(String name, String phone, double money, Log log)
	{
		boolean isFlag=false;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		BaseDao bd=new BaseDao();
		conn=bd.getConnection();
		if(conn!=null)
		{
			String sql="insert into user(name,phone,money,fk_per_id,fk_log_id) values(?,?,?,?,?)";		
			try
			{
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, name);
				pstmt.setString(2, phone);
				pstmt.setDouble(3, money);
				pstmt.setInt(4,2);
				pstmt.setInt(5, log.getId());
				int num=pstmt.executeUpdate();
				if(num>0)
				{
					isFlag=true;
				}
				
			} catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				bd.closeConnection(conn, pstmt, rs);
			}

		}
		return isFlag;
	}

	@Override
	public boolean updateUser(User user, String name, String phone, double money)
	{
		boolean isFlag=false;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		BaseDao bd=new BaseDao();
		conn=bd.getConnection();
		
		
		
		if(conn!=null)
		{
			String sql="update user set name=?,phone=?,money=? where id=?";		
			try
			{
				pstmt=conn.prepareStatement(sql);
				
				if(!name.equals("")){
					pstmt.setString(1, name);
				}else{
					pstmt.setString(1, user.getName());
				}
				
				if(!phone.equals("")){
					pstmt.setString(2, phone);
				}else{
					pstmt.setString(2, user.getPhone());
				}
				
				pstmt.setDouble(3, user.getMoney()+money);
				
				pstmt.setInt(4,user.getId());
				
				
				int num=pstmt.executeUpdate();
				if(num>0)
				{
					isFlag=true;
				}
				
			} catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				bd.closeConnection(conn, pstmt, rs);
			}

		}
		return isFlag;
	}

}
